package cn.teduymdj.jxc.controller;


import cn.teduymdj.jxc.myAnnotation.LogAnnotation;
import cn.teduymdj.jxc.pojo.dto.ReturnListAddNewDTO02;
import cn.teduymdj.jxc.pojo.dto.SelectReturnCondition;
import cn.teduymdj.jxc.pojo.dto.SelectReturnListCondition;
import cn.teduymdj.jxc.pojo.entity.Log;
import cn.teduymdj.jxc.pojo.vo.PurchaseListCountListItemVO;
import cn.teduymdj.jxc.pojo.vo.ReturnListGoodsListItemVO;
import cn.teduymdj.jxc.pojo.vo.ReturnListListItemVO;
import cn.teduymdj.jxc.service.ReturnListGoodsService;
import cn.teduymdj.jxc.util.CreateNumUtil;
import cn.teduymdj.jxc.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 退货商品Controller层
 */
@Slf4j
@RestController
@RequestMapping("/returnListGoods")
public class ReturnListGoodsController {

    @Autowired
    private ReturnListGoodsService returnListGoodsService;


    /**
     * 保存退货单信息
     *
     * @param returnList         退货单信息实体
     * @param returnListGoodsStr 退货商品信息JSON字符串
     * @return
     */
    //日志保存注解,不用管:李柯辰
    @LogAnnotation(content = "保存退货单信息", type = Log.INSERT_ACTION)
    @RequestMapping("/save")
    public Map<String, Object> save(@RequestBody ReturnListAddNewDTO02 returnList) {
        log.debug("{}", returnList);
        returnListGoodsService.save(returnList);
        return null;
    }

    /**
     * 查询退货单
     *
     * @param returnNumber 单号
     * @param supplierId   供应商ID
     * @param state        付款状态
     * @param sTime        开始时间
     * @param eTime        结束时间
     * @return
     */
    @LogAnnotation(content = "查询退货单", type = Log.SELECT_ACTION)
    @RequestMapping("/list")
    public JsonResult<List<ReturnListListItemVO>> list(@RequestBody SelectReturnListCondition selectReturnListCondition) {

        List<ReturnListListItemVO> returnListList = returnListGoodsService.getReturnlist(selectReturnListCondition);

        return JsonResult.ok(returnListList);
    }

    /**
     * 查询退货单商品信息
     *
     * @param returnListId 退货单ID
     * @return
     */
    @LogAnnotation(content = "查询退货单商品信息", type = Log.SELECT_ACTION)
    @RequestMapping("/goodsList")
    public JsonResult<List<ReturnListGoodsListItemVO>> goodsList(Long returnListId) {

        List<ReturnListGoodsListItemVO> returnListGoodsList = returnListGoodsService.getReturnListGoodsByReturnListId(returnListId);

        return JsonResult.ok(returnListGoodsList);
    }

    /**
     * 删除退货单及商品信息
     *
     * @param returnListId 退货单ID
     * @return
     */
    @LogAnnotation(content = "删除退货单及商品信息", type = Log.DELETE_ACTION)
    @RequestMapping("/delete")
    public JsonResult<Void> delete(Long returnListId) {

        returnListGoodsService.delete(returnListId);

        return JsonResult.ok();
    }



    /**
     * 修改退货单付款状态
     *
     * @param id 进货单ID
     * @return
     */
    @LogAnnotation(content = "修改退货单付款状态为付款", type = Log.UPDATE_ACTION)
    @PostMapping("/{id:[0-9]+}/Paid")
    public JsonResult<Void> setPaid(@PathVariable Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        returnListGoodsService.setPaid(id);
        return JsonResult.ok();
    }

    /**
     * 修改进货单付款状态
     *
     * @param id 进货单ID
     * @return
     */
    @LogAnnotation(content = "修改退货单付款状态为未付款", type = Log.UPDATE_ACTION)
    @PostMapping("/{id:[0-9]+}/Unpaid")
    public JsonResult<Void> setUnpaid(@PathVariable Long id) {
        log.debug("开始处理【禁用管理员】的请求，参数：{}", id);
        returnListGoodsService.setUnpaid(id);
        return JsonResult.ok();
    }

    /**
     * 进货商品统计
     *
     * @param sTime       开始时间
     * @param eTime       结束时间
     * @param goodsTypeId 商品类别ID
     * @param codeOrName  编号或商品名称
     * @return
     */
    @LogAnnotation(content = "退货商品统计", type = Log.SELECT_ACTION)
    @RequestMapping("/count")
    public JsonResult<List<PurchaseListCountListItemVO>> count(@RequestBody SelectReturnCondition selectReturnCondition) {

        List<PurchaseListCountListItemVO> returnListGoodsList =
                returnListGoodsService.getReturnListGoodsByCondition(selectReturnCondition);
        return JsonResult.ok(returnListGoodsList);
    }

    @RequestMapping("/getNum")
    public JsonResult<String> getNum(){
        return JsonResult.ok(CreateNumUtil.RetNum());
    }

}
